package com.struct.data
{
	import com.collection.ActionCollection;
	import com.collection.FactoryCollection;
	import com.utils.Utils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import nochump.util.zip.ZipEntry;
	import nochump.util.zip.ZipFile;
	
	import sacu.s.worker.DataWorker;
	
	public class ZIPDataWorker extends DataWorker
	{
		private var urlLoader:URLLoader;
		private var i:uint;
		public function ZIPDataWorker(name:String, command:String)
		{
			super(name, command);
			urlLoader = new URLLoader();
			urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			urlLoader.addEventListener(Event.COMPLETE, completeHandler);
		}
		public function update2origin():void{
			var ur:URLRequest = new URLRequest("res/img.zip");
//			var ur:URLRequest = new URLRequest("res/data.bin");
			urlLoader.load(ur);
		}
		private function completeHandler(e:Event):void{
			var data:ByteArray = urlLoader.data;
			//用zip解析
			var zip:ZipFile = new ZipFile(data);
			var loader:Loader = new Loader();
			var temp:Dictionary = new Dictionary();
			state = 0;
			if(zip.size > 0){
				FactoryCollection.loader.startFactory();
				dispatchAction(ActionCollection.CONFIG_LOADER, {name:"ZIP加载信息", destroy:false, validator:ActionCollection.ZIP_DATA, total:zip.size});
				i = 0;
				var entry:ZipEntry = zip.entries[i];
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function loaderCompleteHandler(e:Event):void{
					var tempName:String = entry.name;
					dispatchAction(ActionCollection.DATA_LOADER, {name:tempName, validator:ActionCollection.ZIP_DATA});
					temp[tempName.substr(0, tempName.indexOf("."))] = e.target.loader.content.bitmapData
					if(++i == zip.size){
						loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderCompleteHandler);
						onloadSucess(temp);
					} else {
						entry = zip.entries[i];
						loader.loadBytes(zip.getInput(entry));
					}
				});
				loader.loadBytes(zip.getInput(entry));
			}
		}
		public function disposeLoader():void{
			urlLoader.removeEventListener(Event.COMPLETE, completeHandler);
		}
	}
}